# -*- coding: utf-8 -*-
import scrapy
from spCrawl.items import douyu
import json

class DouyuSpider(scrapy.Spider):
    name = 'douyu'
    allowed_domains = ['douyucdn.cn']

    url = "http://capi.douyucdn.cn/api/v1/getVerticalRoom?limit=20&offset="
    offset = 0
    start_urls = [url + str(offset)]

    # http://capi.douyucdn.cn/api/v1/getVerticalRoom?limit=20&offset=20
    def parse(self, response):
        data = json.loads(response.text)["data"]
        for each in data:
            item = douyu()
            item["nickname"] = each["nickname"]
            item["imagelink"] = each["room_src"]
            # 返回给pipeline处理
            yield item
        # if self.offset < 2:
        self.offset += 1

        yield scrapy.Request(self.url + str(self.offset), callback=self.parse)
